/**** COMMON ****/
var b_gaid="UA-12420642-2";
if(!$tt) {
	//attempt to load ga library
	try{
		if(!_gaq) {
			var $B_gaq = _gaq || [];
			$B_gaq.push(['_setAccount', b_gaid]);
			
			(function() {
				var $B_ga = document.createElement('script'); $B_ga.type = 'text/javascript'; $B_ga.async = true;
				$B_ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
				var $B_s = document.getElementsByTagName('script')[0]; $B_s.parentNode.insertBefore($B_ga, $B_s);
			})();
		} else {
			var $B_gaq = _gaq;
		}
	} catch(e) { /*ignore*/ }
	
	if(!$tt){
		if(turntable){ var $tt=turntable; }//grab turntable object
		if($tt){
			for(i in $tt){
				var $tt_roomkey=$tt[i]; break; //grab roomkey object
			}
		}
		if($tt_roomkey){
			for(i in $tt_roomkey){
				if($tt_roomkey[i]&&$tt_roomkey[i].callback) {
					var $tt_vote=$tt_roomkey[i]; break; //grab object for callback
				}
			}
		}
	}
}

if(!autoStatusDiv) {
	var autoStatusDiv=$('#bcj19-tt').html();
	if(!autoStatusDiv) {
		$('div.header-content').append('<div id="bcj19-tt"><table><tr id="bcj19-tt-tr"><td id="bcj19-tt-awesome" title="Auto-Awesome">&nbsp;</td><td id="bcj19-tt-dj" title="Auto-DJ">&nbsp;</td><td id="bcj19-tt-skip" title="Auto-Skip">&nbsp;</td></tr></table>');
	}
}
/**** END COMMON ****/
var ttImagePath="http://bcj19-tt.googlecode.com/svn/trunk/images/";
var tcDefaultSecs = 15; //default seconds for timedCount

/*set timed interval for voting*/
function timedCount(pv_secs,pv_enable) {
	if(pv_enable) {
		(pv_secs) ? pv_secs = pv_secs * 1000 : pv_secs = tcDefaultSecs * 1000; //convert secs to milliseconds
		var t = setTimeout("clickTheButton(ttAwesomeEnabled)", pv_secs); //vote and re-start timedCount interval
	} else { clearTimeout(t); }
}
/*vote and re-start timed interval*/
function clickTheButton(pv_enabled) {
	if(pv_enabled) {
		var tcSecs = (tcDefaultSecs) ? tcDefaultSecs : Math.floor(Math.random() * 10) + "" + Math.floor(Math.random() * 10); //random timedCount duration (1-99 seconds)
	
        try{ $(window).mousemove(); } catch(e) { /*ignore*/ } //idle timer|afk reset
        
        $tt_vote.callback("upvote"); //vote
		//changeDjAvatarCustom();
		//changeAvatarTT();
		timedCount(tcSecs,true); //set timer for next vote interval
	} else { 
		timedCount('',false);
	}
}
/*send first vote and initiate timed interval loop*/
if(ttAwesomeEnabled) { 
	ttAwesomeEnabled=false; 
	$('#bcj19-tt-awesome').css('background-color','');$('#bcj19-tt-awesome').attr('title','Auto-Awesome Disabled');
	
	//send ga call
	try{
		$B_gaq.push(['_setAccount', b_gaid])
		$B_gaq.push(['_trackEvent','Auto-Awesome','Disabled']);
		
		$B_gaq.push(['_setAccount', 'UA-20455078-4']);
	} catch(e) { /*ignore*/ }
} else { 
	var ttAwesomeEnabled=true;
	$('#bcj19-tt-awesome').css('background-color','#00ff00');$('#bcj19-tt-awesome').attr('title','Auto-Awesome Enabled');
	
	//send ga call
	try{
		$B_gaq.push(['_setAccount', b_gaid]);
		$B_gaq.push(['_trackEvent','Auto-Awesome','Enabled']);
		
		$B_gaq.push(['_setAccount', 'UA-20455078-4']);
	} catch(e) { /*ignore*/ }
}
/*update dj avatar (headfront)*/
function changeDjAvatarCustom() {
	try{
		var ttAvatarHeadFront;
		var ttAvatarPath=ttImagePath;
		
		for(ii in $tt_vote.djs) {
			if($tt_vote.djs[ii][0]=="4ebaa9874fe7d035d50000a0") { ttAvatarHeadFront="djChopStixHeadFront.png"; } //ms
			else if($tt_vote.djs[ii][0]=="4ea1ccc84fe7d03fab06f406") { ttAvatarHeadFront="djAppollonarisHeadFront.png"; } //ww-appolinaris
			else if($tt_vote.djs[ii][0]=="4de63da1e8a6c474010010b1") { ttAvatarHeadFront="wallaceNewBunnyHeadFront.png"; } //ww|previous=116 (60)
			else { ttAvatarHeadFront=""; }
			
			if(ttAvatarHeadFront) {
				var def=$tt_vote.djs[ii][1];
				ttAvatarHeadFront=ttAvatarPath+ttAvatarHeadFront;
				if($tt_vote.djs[ii][1].layers) { $tt_vote.djs[ii][1].layers.headfront.src=ttAvatarHeadFront; }
				if($tt_vote.djs[ii][1].parts) { $tt_vote.djs[ii][1].parts.headfront.src=ttAvatarHeadFront; }
			}
		} 
	} catch(e) { /*console.log('ERROR updating dj avatar');*/ }
}
/*change dj avatars based on available tt avatar ids*/
function changeAvatarTT(){
	if($tt_roomkey.users["4ea1b9cb4fe7d03f9f06be81"]) {
		if($tt_roomkey.users["4ea1b9cb4fe7d03f9f06be81"].avatarid!=234) {$tt_roomkey.messageListener({command: "update_user",userid: "4ea1b9cb4fe7d03f9f06be81", avatarid: 234}) }	//bcj|previous=91|42|234
	}
	if($tt_roomkey.users["4eb330664fe7d020bf000abc"]) {
		if($tt_roomkey.users["4eb330664fe7d020bf000abc"].avatarid!=131) {$tt_roomkey.messageListener({command: "update_user",userid: "4eb330664fe7d020bf000abc", avatarid: 131}) }	//bcj-at
	}
	if($tt_roomkey.users["4ef3d4f0590ca23e330002e6"]) {
		if($tt_roomkey.users["4ef3d4f0590ca23e330002e6"].avatarid!=272) {$tt_roomkey.messageListener({command: "update_user",userid: "4ef3d4f0590ca23e330002e6", avatarid: 272}) }	//bcj-19|previous=100
	}
	if($tt_roomkey.users["4f22c623a3f75176b40025e9"]) {
		if($tt_roomkey.users["4f22c623a3f75176b40025e9"].avatarid!=144) {$tt_roomkey.messageListener({command: "update_user",userid: "4f22c623a3f75176b40025e9", avatarid: 144}) }	//jmj
	}
    if($tt_roomkey.users["4ea1c5874fe7d03f9f06f634"]){
		if($tt_roomkey.users["4ea1c5874fe7d03f9f06f634"].avatarid!=143) {$tt_roomkey.messageListener({command: "update_user",userid: "4ea1c5874fe7d03f9f06f634", avatarid: 143}) }	//gm|previous=custom
	}
    if($tt_roomkey.users["4eaaca414fe7d07b2b058499"]){
		if($tt_roomkey.users["4eaaca414fe7d07b2b058499"].avatarid!=96) {$tt_roomkey.messageListener({command: "update_user",userid: "4eaaca414fe7d07b2b058499", avatarid: 96}) }	//mr|previous=custom
	}
	if($tt_roomkey.users["4eb063494fe7d077720001cb"]){
		if($tt_roomkey.users["4eb063494fe7d077720001cb"].avatarid!=51) {$tt_roomkey.messageListener({command: "update_user",userid: "4eb063494fe7d077720001cb", avatarid: 51}) }	//tw|previous=51|331
	}
	if($tt_roomkey.users["4eb19f334fe7d020a40001ce"]){
		if($tt_roomkey.users["4eb19f334fe7d020a40001ce"].avatarid!=261) {$tt_roomkey.messageListener({command: "update_user",userid: "4eb19f334fe7d020a40001ce", avatarid: 261}) }	//cw|previous=58
	}
	if($tt_roomkey.users["4ef49c0c590ca23e2f0006bf"]){
		if($tt_roomkey.users["4ef49c0c590ca23e2f0006bf"].avatarid!=142) {$tt_roomkey.messageListener({command: "update_user",userid: "4ef49c0c590ca23e2f0006bf", avatarid: 142}) }	//tw-sma
	}
	if($tt_roomkey.users["4e5e6325a3f7514dfb03342d"]){
		if($tt_roomkey.users["4e5e6325a3f7514dfb03342d"].avatarid!=165) {$tt_roomkey.messageListener({command: "update_user",userid: "4e5e6325a3f7514dfb03342d", avatarid: 165}) }	//glifex
	}
	if($tt_roomkey.users["4e526711a3f7510445225ca9"]){
		if($tt_roomkey.users["4e526711a3f7510445225ca9"].avatarid!=75) {$tt_roomkey.messageListener({command: "update_user",userid: "4e526711a3f7510445225ca9", avatarid: 75}) }	//pep
	}
	if($tt_roomkey.users["4e02188ca3f75103a200cd46"]){
		if($tt_roomkey.users["4e02188ca3f75103a200cd46"].avatarid!=52) {$tt_roomkey.messageListener({command: "update_user",userid: "4e02188ca3f75103a200cd46", avatarid: 52}) }	//morticia
	}
	if($tt_roomkey.users["4ef24487590ca203720002fb"]){
		if($tt_roomkey.users["4ef24487590ca203720002fb"].avatarid!=148) {$tt_roomkey.messageListener({command: "update_user",userid: "4ef24487590ca203720002fb", avatarid: 148}) }	//mr-spawn of marc
	}
	if($tt_roomkey.users["500f0c2aaaa5cd671e000285"]){
		if($tt_roomkey.users["500f0c2aaaa5cd671e000285"].avatarid!=150) {$tt_roomkey.messageListener({command: "update_user",userid: "500f0c2aaaa5cd671e000285", avatarid: 150}) }	//djgio-friend of pep
	}
}
if($tt) { clickTheButton(ttAwesomeEnabled); }